<template>
    <div id="login">
        <Title title="用户登录"/>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item prop="pass">
                <el-input prefix-icon="el-icon-user"  type="text" v-model="ruleForm.pass" placeholder="账户" autocomplete="off"/>
            </el-form-item>
            <el-form-item  prop="checkPass">
                <el-input type="password" prefix-icon="el-icon-lock" v-model="ruleForm.checkPass" autocomplete="off" placeholder="密码"/>
            </el-form-item>

            <el-form-item style="width: 550px">
                <div class="check">
                    <el-checkbox>自动登录</el-checkbox>
                </div>

                <div class="link">
                    <el-link type="primary" :underline="false">基地注册</el-link>
                    <el-link type="primary" :underline="false">忘记密码</el-link>
                </div>

            </el-form-item>
            <el-form-item style="width: 350px" class="login-btn">
                <el-button  type="primary" style="width: 350px">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {Form, FormItem, Input, Button, Checkbox, Link} from "element-ui";
    import Title from "../../../../component/title/Title";

    export default {
        name: "Login",
        components: {
            "el-form": Form,
            "el-form-item": FormItem,
            "el-input": Input,
            "el-button": Button,
            "el-checkbox": Checkbox,
            "el-link": Link,
            Title
        },
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    age: ''
                },
                rules: {
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    age: [
                        {validator: checkAge, trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {

                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style lang="less">
    #login {
        margin-top: 50px;
        width: 450px;
        .title {
            border-bottom: none;
            margin-left: 100px;
        }
        .demo-ruleForm{
            margin-top: 25px;
            .check{
                display: inline-block;
            }
            .link{
                margin-left: 120px;
                display: inline-block;
                span{
                    margin-left: 10px;
                }
            }
            .login-btn{
                margin-top: -10px;
            }
        }
    }
</style>
